home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / net / hostent.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  64 lines

  1. /*
  2.  * Print the "hostent" information for every host whose name is
  3.  * specified on the command line.
  4.  */
  5.  
  6. #include    <stdio.h>
  7. #include    <sys/types.h>
  8. #include    <netdb.h>        /* for struct hostent */
  9. #include    <sys/socket.h>        /* for AF_INET */
  10. #include    <netinet/in.h>        /* for struct in_addr */
  11. #include    <arpa/inet.h>        /* for inet_ntoa() */
  12.  
  13. main(argc, argv)
  14. int    argc;
  15. char    **argv;
  16. {
  17.     register char        *ptr;
  18.     char            *host_err_str();    /* our lib function */
  19.     register struct hostent    *hostptr;
  20.  
  21.     while (--argc > 0) {
  22.         ptr = *++argv;
  23.         if ( (hostptr = gethostbyname(ptr)) == NULL) {
  24.             err_ret("gethostbyname error for host: %s %s",
  25.                     ptr, host_err_str());
  26.             continue;
  27.         }
  28.         printf("official host name: %s\n", hostptr->h_name);
  29.  
  30.         /* go through the list of aliases */
  31.         while ( (ptr = *(hostptr->h_aliases)) != NULL) {
  32.             printf("    alias: %s\n", ptr);
  33.             hostptr->h_aliases++;
  34.         }
  35.         printf("    addr type = %d, addr length = %d\n",
  36.                 hostptr->h_addrtype, hostptr->h_length);
  37.  
  38.         switch (hostptr->h_addrtype) {
  39.         case AF_INET:
  40.             pr_inet(hostptr->h_addr_list, hostptr->h_length);
  41.             break;
  42.  
  43.         default:
  44.             err_ret("unknown address type");
  45.             break;
  46.         }
  47.     }
  48. }
  49.  
  50. /*
  51.  * Go through a list of Internet addresses,
  52.  * printing each one in dotted-decimal notation.
  53.  */
  54.  
  55. pr_inet(listptr, length)
  56. char    **listptr;
  57. int    length;
  58. {
  59.     struct in_addr    *ptr;
  60.  
  61.     while ( (ptr = (struct in_addr *) *listptr++) != NULL)
  62.         printf("    Internet address: %s\n", inet_ntoa(*ptr));
  63. }
  64.